﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using XieCan.Core.DTO;
using XieCan.Core.Entity;
using XieCan.Core.IReposity;
using XieCan.Core.JWT;
using XieCan.Core.Swigger;

namespace XieCan.Core.Api
{
  /// <summary>
  /// 控制器基类
  /// </summary>
  /// <typeparam name="TController">控制器类型</typeparam>
  /// <typeparam name="TEntity">数据实体</typeparam>
  /// <typeparam name="TCreateModel">添加模型</typeparam>
  /// <typeparam name="TUpdateModel">修改模型</typeparam>
  /// <typeparam name="TListModel">查询列表结果模型</typeparam>
  /// <typeparam name="TSingleModel">查询数据结果模型</typeparam>
  /// <remarks>
  /// 构造函数
  /// </remarks>
  /// <param name="loggerFactory">日志工厂</param>
  /// <param name="repository">数据仓储</param>
  /// <param name="mapper">数据映射</param>
  [Authorize]
  [ApiController]
  public abstract class ApiControllerBase<TController, TEntity, TCreateModel, TUpdateModel, TListModel, TSingleModel>(ILoggerFactory loggerFactory, IBaseRepository<TEntity, TCreateModel, TUpdateModel, TListModel, TSingleModel> repository, IMapper mapper) : ControllerBase
    where TController : ControllerBase
    where TEntity : BaseEntity, new()
    where TCreateModel : BaseCreateModel, new()
    where TUpdateModel : BaseUpdateModel, new()
    where TListModel : BaseListModel, new()
    where TSingleModel : BaseSingleModel, new()
  {
    #region 属性
    /// <summary>
    /// log4net日志
    /// </summary>
    protected readonly ILogger logger = loggerFactory.CreateLogger<TController>();
    protected readonly IBaseRepository<TEntity, TCreateModel, TUpdateModel, TListModel, TSingleModel> repository = repository;
    protected readonly IMapper mapper = mapper;
    #endregion

    #region 公开方法

    /// <summary>
    /// 查询全部
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public virtual async Task<OperateResult<List<TListModel>>> GetList()
    {
      return await repository.GetListAsync(item => true);
    }

    ///// <summary>
    ///// 查询分页
    ///// </summary>
    ///// <param name="page">当前页码</param>
    ///// <param name="size">分页大小</param>
    ///// <returns></returns>
    //[HttpGet]
    //public virtual async Task<OperateResult<PagedList<TListModel>>> GetPagedList(int page = 1, int size = 10)
    //{
    //  return await repository.GetPagedListAsync(item => true, page, size);
    //}

    /// <summary>
    /// 根据id查询单个
    /// </summary>
    /// <param name="id">唯一标识</param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public virtual async Task<OperateResult<TSingleModel?>> GetById(Guid id)
    {
      return await repository.GetAsync(id);
    }

    /// <summary>
    /// 添加单个
    /// </summary>
    /// <param name="model"></param>
    [HttpPost]
    public virtual async Task<OperateResult<bool>> Post([FromBody] TCreateModel model)
    {
      return await repository.InsertAsync(model);
    }

    /// <summary>
    /// 根据id修改单个
    /// </summary>
    /// <param name="id">唯一标识</param>
    /// <param name="model"></param>
    [HttpPut("{id}")]
    public virtual async Task<OperateResult<bool>> Put(Guid id, [FromBody] TUpdateModel model)
    {
      return await repository.UpdateAsync(id, model);
    }

    /// <summary>
    /// 根据id删除单个
    /// </summary>
    /// <param name="id">唯一标识</param>
    [HttpDelete("{id}")]
    public virtual async Task<OperateResult<bool>> DeleteById(Guid id)
    {
      return await repository.DeleteAsync(id, "");
    }

    #endregion
  }

  [ApiRoute(ApiVersion.V2)]
  public class FileController : ControllerBase
  {
    [HttpPost]
    public IActionResult Login()
    {
      // 1. 定义需要使用到的Claims
      var claims = new[]
      {
        new Claim("Id", "9527"),
        new Claim("Name", "Admin")
      };

      // 7. 将token变为string
      var jwtToken = JWTHelper.CreateToken(claims, DateTime.Now.AddSeconds(30));

      return Ok(jwtToken);
    }

    [Authorize]
    [HttpPost]
    public virtual void Post([FromBody] IFormFile file)
    {
    }
  }
}
